package com.jlect.swebing.ui.client.button;

import com.jlect.swebing.renderers.client.Image;
import com.jlect.swebing.ui.client.*;

/**
 * Toggle button implementation
 *
 * @author Sergey Kozmin
 * @since 23.11.2007 10:49:43
 */
public class GToggleButton extends GContainer {
    private ButtonBase base;
    private MouseEventsRedirector redirector = new MouseEventsRedirector(this);

    protected GToggleButton(String text, Image image) {
        setLayoutManager(new FlowLayout());
        GLabel label = new GLabel(text);
        addComponent(label);

        //redirect mouse events to toggle button listeners
        label.addMouseListener(redirector);

        ButtonView view = new ToggleButtonViewImpl(this);
        ToggleButtonRendererAdapter toggleButtonRendererAdapter = new ToggleButtonRendererAdapter(label);
//        toggleButtonRendererAdapter.setActionListener(this);
        view.setButtonComponent(toggleButtonRendererAdapter);
        ButtonModel model = new DefaultButtonModelImpl(this);
        base = new ButtonBase(model, view, new ToggleButtonControllerImpl());

        model.setButtonText(text);
        model.setButtonImage(image);

        setPreferredSize(new Dimension(120, 20));//todo get font metrix
    }

    public GToggleButton(String text) {
        this(text, null);
    }

    public GToggleButton(Image image) {
        this(null, image);
    }

    public void addActionListener(ActionListener listener) {
        base.getController().addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        base.getController().removeActionListener(listener);
    }
}
